package com.lanswon.qzsmk.util.inventoryUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanswon.qzsmk.dao.CardBatchMapper;
import com.lanswon.qzsmk.dao.CardControlMapper;
import com.lanswon.qzsmk.model.CardBatch;
import com.lanswon.qzsmk.model.CardControl;
import com.lanswon.qzsmk.util.CommonUtil;
import com.lanswon.qzsmk.util.FileUtil;
import com.mysql.fabric.xmlrpc.base.Array;

import ch.qos.logback.classic.Logger;

@Component
public class ParseFilePath {
	
	private static Logger logger = (Logger) LoggerFactory.getLogger(ParseFilePath.class);
	@Autowired
	CardControlMapper ccm;
	@Autowired
	CardBatchMapper cbm;
	
	public List<CardControl> parseDistributeTaskFile(List<String> list,String director){
		
		List<String> citizenCardNoList = new ArrayList<String>();
		List<CardControl> controlObjList = null;
		for(int i =0;i<list.size();i++){
			String distributeNo = list.get(i);
			String path = director + File.separator + distributeNo + ".txt";
			List<String> citizenNoList = subParseFile(path);
			citizenCardNoList.addAll(citizenNoList);
		}
		//通过市民卡号查询controlID
		controlObjList = ccm.queryCardControlByCitizenNoList(citizenCardNoList);
		return controlObjList;
	}
	
	private List<String> subParseFile(String path) {
		List<String> citizenNoList = new ArrayList<String>();
		File file = new File(path);
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		List<String> lineList = new ArrayList<String>();
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis,"UTF-8");
			br = new BufferedReader(isr);
			String line = null;
			while((line=br.readLine())!=null){
				String[] lineArray = line.split("\\|");
				citizenNoList.add(lineArray[1]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(isr!=null){
				try {
					isr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
		return citizenNoList;
	}

	public void moveFile(List<String> distributeTaskNoList, String sourceBasePath, String targeBasetPath) {
		for(int i =0;i<distributeTaskNoList.size();i++){
			String distributeNo = distributeTaskNoList.get(i);
			String sourcePath = sourceBasePath + File.separator + distributeNo + ".txt";
			String targetPath = targeBasetPath;
			FileUtil.moveTotherFolders(sourcePath, targetPath);
		}
	}

	public void deleteFile(List<String> distributeTaskNoList, String sourceBasePath) {
		
		for(int i =0;i<distributeTaskNoList.size();i++){
			String distributeNo = distributeTaskNoList.get(i);
			String sourcePath = sourceBasePath + File.separator + distributeNo + ".txt";
			File file = new File(sourcePath);
			if (file.exists()) {
				file.delete();
			}
		}	
	}

	public List<String> readMakeCardFile(List<CardBatch> cardBatchList, String sourceSuccessPath) {

		List<String> fileInfoList = new ArrayList<String>();			
		List<String> citizenCardNoList = new ArrayList<String>();
	//	List<String> rbjmCitizenCardNoList = new ArrayList<String>();
		for(int i=0;i<cardBatchList.size();i++){
			String companyName = cardBatchList.get(i).getCooperationCompany().getCompanyName();
			//将R转换成Q
			String fileName = "Q"+ cardBatchList.get(i).getFileName().substring(1);
			String fileType = cardBatchList.get(i).getFileName().substring(0,4);
	//		String cardBatchNo = cardBatchList.get(i).getBatchMarkerNo();
			String sourcePath = sourceSuccessPath+File.separator+companyName+File.separator+fileName;
			logger.debug("sourcePath==>"+sourceSuccessPath);
			logger.debug("fileType==>"+fileType);
			
			if("RJMK".equals(fileType)){
				//读取文件
				File targetFile = new File(sourcePath);
				
				if(targetFile.exists()){		
					FileInputStream fis = null;
					InputStreamReader isr = null;
					BufferedReader br = null;
					try {
						fis = new FileInputStream(targetFile);
						isr = new InputStreamReader(fis, "GBK");
						br = new BufferedReader(isr);
						String line = "";
						while ((line = br.readLine()) != null) {
							fileInfoList.add(line);
				        }  
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally{
						if(br!=null){
							try {
								br.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						if(isr!=null){
							try {
								isr.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						if(fis!=null){
							 try {
								fis.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
			
			/*if("RBJM".equals(fileType)){
				//通过数据库检索信息 cardBatchNo
				List<String> rbjmCitizenCardNo = cbm.queryCitizenCardNoListByBatchMakeNo(cardBatchNo);
				rbjmCitizenCardNoList.addAll(rbjmCitizenCardNo);
			}*/
			
		}
		
		//解析出市民卡号
		if(fileInfoList.size()>0){
			for(int j=1;j<fileInfoList.size();j++){
				String citizenCardNoAsc = fileInfoList.get(j).split("\\|")[3];
				String citizenCardNo = CommonUtil.AsciiStringToString(citizenCardNoAsc);
				citizenCardNoList.add(citizenCardNo);
			}
		}
		/*logger.debug("rbjmCitizenCardNoList==>"+rbjmCitizenCardNoList.size());
		if(rbjmCitizenCardNoList.size()>0){
			citizenCardNoList.addAll(rbjmCitizenCardNoList);
		}*/
		return citizenCardNoList;
		
	}
	
	public void moveMakeCardFile(List<CardBatch> cardBatchList, String sourceBasePath, String targeBasetPath) {

					
		
		for(int i=0;i<cardBatchList.size();i++){
			String companyName = cardBatchList.get(i).getCooperationCompany().getCompanyName();
			//将R转换成Q
			String fileName = "Q"+ cardBatchList.get(i).getFileName().substring(1);
			String sourcePath = sourceBasePath+File.separator+companyName+File.separator+fileName;
			logger.debug("sourcePath==>"+sourcePath);
			String targetPath = targeBasetPath+File.separator+companyName;
			logger.debug("targetPath==>"+targetPath);
			FileUtil.moveTotherFolders(sourcePath,targetPath);
		}
		
	}


}
